package main

import (
	"fmt"
	"sync"
	"time"
)
var wg sync.WaitGroup
var mutex sync.RWMutex

func write() {
	// 只写一个 写互斥
	mutex.Lock()
	fmt.Println("write")
	time.Sleep(1 * time.Second)
	mutex.Unlock()
	wg.Done()
}

func read() {
	// 读多个 读并行
	mutex.RLock()
	fmt.Println("read")
	time.Sleep(1 * time.Second)
	mutex.RUnlock()
	wg.Done()
}

func main() {
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go write()
	}
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go read()
	}
	wg.Wait()
}